﻿using Dapper;
using System;
using BCEntity.Common.Business.WebsiteBusinessTagcode;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.Common.Business.WebsiteBusinessTagcode
{
    /// <summary>
    /// 官网业务标记码
    /// </summary>
    public class WebsiteBusinessTagcodeData : IWebsiteBusinessTagcodeData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM websitebusinesstagcode";
        private const string QUERY = @"SELECT WebsiteBusinessTagcode,Description,IsSystem,Enabled,CreateAdminId,CreateAdminName,CreateTime,UpdateTime,EditAdminId,EditAdminName FROM websitebusinesstagcode";
        public WebsiteBusinessTagcodeData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #region 检查官网业务标记码是否存在
        public async Task<bool> Exists(string websiteBusinessTagcode)
        {
            string sql = string.Format(@"{0} WHERE WebsiteBusinessTagcode=?WebsiteBusinessTagcode LIMIT 1", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteBusinessTagcode", websiteBusinessTagcode, DbType.AnsiString, size: 50);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        #region 添加官网业务标记码
        public async Task<WebsiteBusinessTagcodeEntity> Add(WebsiteBusinessTagcodeEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("websitebusinesstagcode", new string[] { "WebsiteBusinessTagcode", "Description", "IsSystem", "CreateAdminId", "CreateAdminName", });
            sql = string.Format("{0} {1} WHERE WebsiteBusinessTagcode=?WebsiteBusinessTagcode;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteBusinessTagcode", entity.WebsiteBusinessTagcode?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?Description", entity.Description?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?IsSystem", entity.IsSystem, DbType.Boolean);
            parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
            parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WebsiteBusinessTagcodeEntity>(sql, parameters, databaseContext.Transaction);

        }
        #endregion

        #region 修改官网业务标记码
        public async Task<WebsiteBusinessTagcodeEntity> Update(WebsiteBusinessTagcodeEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("websitebusinesstagcode", new string[] { "Description", "EditAdminId", "EditAdminName" }, new string[] { "WebsiteBusinessTagcode" });
            sql = string.Format("{0} {1} WHERE WebsiteBusinessTagcode=?WebsiteBusinessTagcode;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteBusinessTagcode", entity.WebsiteBusinessTagcode?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?Description", entity.Description?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
            parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WebsiteBusinessTagcodeEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion

        #region 删除官网业务标记码
        public async Task<bool> Delete(string websiteBusinessTagcode)
        {
            string sql = @"DELETE FROM websitebusinesstagcode WHERE WebsiteBusinessTagcode=?WebsiteBusinessTagcode";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteBusinessTagcode", websiteBusinessTagcode, DbType.AnsiString, size: 50);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 获取官网业务标记码
        public async Task<WebsiteBusinessTagcodeEntity> GetEntity(string websiteBusinessTagcode)
        {
            string sql = string.Format(@"{0} WHERE WebsiteBusinessTagcode=?WebsiteBusinessTagcode", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteBusinessTagcode", websiteBusinessTagcode, DbType.AnsiString, size: 50);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WebsiteBusinessTagcodeEntity>(sql, parameters);
        }
        #endregion

        #region 获取官网业务标记码列表
        public async Task<IEnumerable<WebsiteBusinessTagcodeEntity>> GetList()
        {
            string sql = string.Format("{0} WHERE Enabled=1", QUERY);
            return await this.databaseContext.Slave.QueryAsync<WebsiteBusinessTagcodeEntity>(sql);
        }
        #endregion

        #region 获取官网业务标记码分页
        public async Task<(IEnumerable<WebsiteBusinessTagcodeEntity>, int)> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();

            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM websitebusinesstagcode {1}",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize,
                  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
            return await this.databaseContext.Slave.QueryMultipleAsync<WebsiteBusinessTagcodeEntity, int>(sql, parameters);
        }
        #endregion

        #region 启用官网业务标记码
        public async Task<bool> Enabled(string websiteBusinessTagcode)
        {
            string sql = @"UPDATE websitebusinesstagcode SET Enabled=1 WHERE WebsiteBusinessTagcode=?WebsiteBusinessTagcode";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteBusinessTagcode", websiteBusinessTagcode, DbType.AnsiString, size: 50);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 禁用官网业务标记码
        public async Task<bool> Disabled(string websiteBusinessTagcode)
        {
            string sql = @"UPDATE websitebusinesstagcode SET Enabled=0 WHERE WebsiteBusinessTagcode=?WebsiteBusinessTagcode";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteBusinessTagcode", websiteBusinessTagcode, DbType.AnsiString, size: 50);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion
    }
}



